﻿
//只可以使用的控制键
function KeyControl(KeyCode) {
    if (((KeyCode >= 8) && KeyCode <= 27) || (KeyCode == 46) || (KeyCode == 144) || (KeyCode >= 33 && KeyCode <= 40)) {
        return true;
    } else {
        return false;
    }
}

//只可以使用数字键
function KeyFloat(KeyCode) {
    if (((KeyCode >= 96) && (KeyCode <= 105)) || ((KeyCode > 47) && (KeyCode < 58)) || (KeyCode == 110) || (KeyCode == 190) || (KeyCode == 8) || (KeyCode == 9)) {
        return true;
    } else {
        return false;
    }
}

//控制整数和小数可输入个数
function KeyFloat2(a, b, keyCode) {
//    if (a.split('.')[0].length > 7) {
//        debugger;
//    }
    //    var cc = a.split('.').length;
    //alert(keyCode);
    //alert(KeyControl(keyCode).toString() + "\n" + (KeyFloat(keyCode) && a.split('.')[0].length < b).toString() + "\n" + (a.split('.')[0].length == b && event.keyCode == 110).toString() + "\n" + (KeyFloat(keyCode) && a.split('.').length > 1).toString());
    if (KeyControl(keyCode) || (KeyFloat(keyCode) && a.split('.')[0].length < b) || (a.split('.')[0].length == b && keyCode == 110) || (KeyFloat(keyCode) && a.split('.').length > 1)) {
        return true;
    } else {
    return false;
    }
}

//判断输入数精度是否匹配以下正则表达式
function regex(val, num) {
    var regex = /^(\d+)?(\.\d{0,2})?$/;
    if (num == 3) regex = /^(\d+)?(\.\d{0,3})?$/;
    if (num == 1) regex = /^(\d+)?(\.\d{0,1})?$/;
    if (regex.test(val)) {
        return true;
    } else {
        return false;
    }
}

function FormatFloat(val, num) {
    if (num == 2) {
        return val.replace(/^(\D{0,})?(\d+)?(\.\d{0,2})?.*$/, '$2$3');
    } else if (num == 3) {
        return val.replace(/^(\D{0,})?(\d+)?(\.\d{0,3})?.*$/, '$2$3');
    } else {
        return val.replace(/^(\D{0,})?(\d+)?(\.\d{0,1})?.*$/, '$2$3');
    }
}

